package com.cw.jeeyt.service.impl.facade;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.service.bo.facade.FacadeEnvBo;
import com.cw.jeeyt.service.converter.facade.FacadeEnvConverter;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.facade.FacadeEnvDao;
import com.cw.jeeyt.dao.model.facade.FacadeEnvDo;
import com.cw.jeeyt.dao.query.facade.FacadeEnvQuery;
import com.cw.jeeyt.service.facade.FacadeEnvService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 环境信息接口业务实现类
 *
 * @author chenw
 * @date 2019-11-01 23:46:50
 */
@Service
public class FacadeEnvServiceImpl implements FacadeEnvService {
    @Autowired
    private FacadeEnvDao facadeEnvDao;

    /**
     * 新增环境信息
     *
     * @param request 条件
     * @return String
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addFacadeEnv(FacadeEnvBo request) {
        if (request == null) {
            return null;
        }
        request.setCreateBy(SessionUtil.getSessionUserId());
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setCreateDate(new Date());
        request.setUpdateDate(new Date());
        facadeEnvDao.insertBySelective(FacadeEnvConverter.bo2Do(request));
        return "";
    }

    /**
     * 更新环境信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean modifyFacadeEnv(FacadeEnvBo request) {
        if (request == null) {
            return false;
        }
        request.setUpdateBy(SessionUtil.getSessionUserId());
        request.setUpdateDate(new Date());
        facadeEnvDao.updateBySelective(FacadeEnvConverter.bo2Do(request));
        return true;
    }

    /**
     * 删除环境信息
     *
     * @param request 条件
     * @return boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteFacadeEnv(FacadeEnvBo request) {
        if (request == null) {
            return false;
        }
        return facadeEnvDao.deleteByKey(request.getId()) > 0;
    }

    /**
     * 分页查询环境信息
     *
     * @param request 条件
     * @return PageInfo<FacadeEnvBo>
     */
    @Override
    public PageInfo<FacadeEnvBo> selectPage(FacadeEnvBo request, PageParam pageParam){
        PageInfo<FacadeEnvDo> pageInfo = facadeEnvDao.selectPage(FacadeEnvQuery.builder()
            .id(request.getId())
            .envCode(request.getEnvCode())
            .envDesc(request.getEnvDesc())
            .ipAddress(request.getIpAddress())
            .port(request.getPort())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build(), pageParam);
        List<FacadeEnvBo> facadeEnvs = FacadeEnvConverter.doList2BoList(pageInfo.getList());
        return new PageInfo<>(pageInfo, facadeEnvs);
    }

    /**
     * 查询环境信息详情信息
     *
     * @param request 条件
     * @return FacadeEnvBo
     */
    @Override
    public FacadeEnvBo queryFacadeEnvDetail(FacadeEnvBo request) {
        if (request == null) {
            return null;
        }
        return FacadeEnvConverter.do2Bo(facadeEnvDao.selectByKey(request.getId()));
    }

    /**
     * 根据条件查询环境信息
     *
     * @param request 条件
     * @return List<FacadeEnvBo>
     */
    @Override
    public List<FacadeEnvBo> queryFacadeEnvList(FacadeEnvBo request) {
        if (request == null) {
            return null;
        }
        return FacadeEnvConverter.doList2BoList(facadeEnvDao.selectByExample(FacadeEnvQuery.builder()
            .id(request.getId())
            .envCode(request.getEnvCode())
            .envDesc(request.getEnvDesc())
            .ipAddress(request.getIpAddress())
            .port(request.getPort())
            .createBy(request.getCreateBy())
            .createDate(request.getCreateDate())
            .updateBy(request.getUpdateBy())
            .updateDate(request.getUpdateDate())
            .build()));
    }

    /**
     * 根据key查询环境信息
     *
     * @param id
     * @return
     */
    @Override
    public FacadeEnvBo selectByKey(Long id) {

        return FacadeEnvConverter.do2Bo(facadeEnvDao.selectByKey(id));
    }
}
